package com.example.gitproject.controller;

import com.example.gitproject.Entity.Book;
import com.example.gitproject.Entity.Result;

import com.example.gitproject.service.BookService;
import com.example.gitproject.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/test")
public class TestController {
    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Autowired
    private BookService bookService;

    @Autowired
    private UserService userService;

    //Result  统一返回响应结果集

    @GetMapping("/findBook")
    public Result findBook(){
        List list = bookService.findAll();
        return new Result(true,"查询成功",list);
    }

    @GetMapping("/findUser")
    public Result findUser(){
        List list = userService.findAllUser();
        return new Result(true,"查询成功",list);
    }


//    @GetMapping("/findBookByName")
//    public Result findBookByName(@RequestParam String name) {
//        List<Book> books = bookService.findByName(name);
//        return new Result(true, "查询成功", books);
//    }

    @GetMapping("/findById")
    public Result findById(@RequestParam Integer id) {
        Book book = bookService.findById(id);
        return new Result(true, "查询成功", book);
    }

    @PostMapping("/updateById")
    public Result updateById(@RequestParam Integer id, @RequestParam String name) {
        int book = bookService.updateById(id, name);
        return new Result(true, "更新成功", book);

    }


    @PostMapping("/delById")
    public Result delById(@RequestParam Integer id) {
        bookService.delById(id);
        return new Result(true, "删除成功");
    }




    //rest风格化接口开发  查询全部user信息
    @GetMapping("/connection")
    public List testConnection(){
        //queryForList 支持多条数据返回
        List<Map<String, Object>> results =   jdbcTemplate.queryForList("SELECT id, name FROM user");
        return results;
    }



    //Result  统一返回响应结果集
    @GetMapping("/connection2")
    public Result testConnection2(){
        //queryForList 支持多条数据返回
        List<Map<String, Object>> results =   jdbcTemplate.queryForList("SELECT id, name FROM user");
        return new Result(true,"查询成功",results);
    }



    @GetMapping("/getBook")
    public Result getBook(){
        //queryForList 支持多条数据返回
        List<Map<String, Object>> results =   jdbcTemplate.queryForList("SELECT * from book  where  author = '屈原'");
        return new Result(true,"查询成功",results);
    }


    //添加书本信息    rest风格化接口开发    put--添加插入   get--查询    post--修改     delete--删除
    @PutMapping("/insertBook")
    public Result insertBook(){
         //  ?-->占位符    sql注入的问题
        String sql = "insert into book  values(?,?,?,?,?);";
        jdbcTemplate.update(sql,4,"三国演义","罗贯中","中国文联出版社","1");
        return new Result(true,"插入成功");
    }



    //修改接口
    @PostMapping("/updateBook")
    public Result updateBook(){
        String sql  = "update book set name = ?,author = ? where id = ?";
        jdbcTemplate.update(sql,"红楼梦","曹雪芹",4);
        return new Result(true,"修改成功");
    }










    @DeleteMapping("/delById")
    public Result updateById(@RequestParam  Integer id){
        bookService.delById(id);
        return new Result(true,"删除成功");
    }



    //添加   @RequestBody 接受表单 对象  json格式数据    参数放在请求体      @RequestParam  一对一键值对的字段信息  拼接在url路径上  参数放在请求头上
    @PostMapping("/insertBook")
    public Result insertBook(@RequestBody Book book){
        bookService.insertBook(book);
        return new Result(true,"添加成功");
    }



}
